package mortal.game.control
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import flash.events.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene.player.entity.*;
    import mortal.game.view.spa.*;
    import mortal.mvc.core.*;

    public class SpaController extends Controller
    {
        private var _spaEntrance:SpaEntranceWindow;
        private var _spaHintButton:SpaHintButton;
        private var _spaInteractPanel:SpaInteractPanel;

        public function SpaController()
        {
            Global.stage.addEventListener(Event.RESIZE, this.onStageResize);
            
        }

        public function get spaEntrance() : SpaEntranceWindow
        {
            if (!this._spaEntrance)
            {
                this._spaEntrance = new SpaEntranceWindow();
            }
            return this._spaEntrance;
        }

        public function get spaHintButton() : SpaHintButton
        {
            if (!this._spaHintButton)
            {
                this._spaHintButton = new SpaHintButton();
                this.onStageResize();
            }
            return this._spaHintButton;
        }

        public function get spaInteractPanel() : SpaInteractPanel
        {
            if (!this._spaInteractPanel)
            {
                this._spaInteractPanel = new SpaInteractPanel();
                this.onStageResize();
            }
            return this._spaInteractPanel;
        }

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.SpaShowSpaEntrance, this.onShowSpaEntrance);
            Dispatcher.addEventListener(EventName.SpaEnterSpa, this.doEnterSpa);
            Dispatcher.addEventListener(EventName.SpaLeaveSpa, this.doLeaveSpa);
            Dispatcher.addEventListener(EventName.SpaBeginSpaInteract, this.onBeginSpaInteract);
            Dispatcher.addEventListener(EventName.SpaRefreshSpaList, this.doRefreshSpaList);
            NetDispatcher.addCmdListener(ServerCommand.SpaGetSpaList, this.onGetSpaList);
            NetDispatcher.addCmdListener(ServerCommand.SpaEnterSpaMap, this.onEnterSpaMap);
            NetDispatcher.addCmdListener(ServerCommand.SpaLeaveSpaMap, this.onLeaveSpaMap);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSpaOpen, this.onSpaOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSpaClose, this.onSpaClose);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSpringInteractCount, this.onIteractNumChange);
            
        }

        private function onShowSpaEntrance(event:DataEvent) : void
        {
            this.showSpaEntrance();
            
        }

        private function doEnterSpa(event:DataEvent) : void
        {
            this.enterSpa(event.data as int);
            
        }

        private function doLeaveSpa(event:DataEvent) : void
        {
            this.leaveSpa();
            
        }

        private function doRefreshSpaList(event:DataEvent) : void
        {
            this.getSpaList();
            
        }

        private function onBeginSpaInteract(event:DataEvent) : void
        {
            var _loc_2:* = event.data as UserPlayer;
            if (_loc_2.entityInfo.entityInfo.entityId)
            {
                GameProxy.spa.springInteract(_loc_2.entityInfo.entityInfo.entityId, cache.spa.interactType);
            }
            
        }

        public function showSpaEntrance() : void
        {
            if (this.spaEntrance.isHide && !cache.spa.isInSpa)
            {
                this.spaEntrance.show();
            }
            else
            {
                this.spaEntrance.hide();
            }
            
        }

        public function enterSpa(param1:int) : void
        {
            GameProxy.copy.enterSpa(param1);
            
        }

        public function leaveSpa() : void
        {
            GameProxy.copy.leftGroup_async();
            
        }

        public function getSpaList() : void
        {
            GameProxy.copy.getSpaList();
            
        }

        private function onGetSpaList(param1:Array) : void
        {
            this.spaEntrance.updateSpaList(param1);
            
        }

        private function onEnterSpaMap(param1:int) : void
        {
            this.spaEntrance.hide();
            this.spaInteractPanel.show();
            this.spaHintButton.removeHighLightEffect();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
            
        }

        private function onLeaveSpaMap(param1:Object) : void
        {
            this.spaInteractPanel.hide();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, true));
            
        }

        private function onSpaOpen(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SSpaOpen;
            this.spaHintButton.setTime(_loc_2);
            this.spaHintButton.show();
            if (cache.role.entityInfo.level >= 30)
            {
                this.spaHintButton.addHighLightEffect();
                if (!cache.copy.isInCopy && !(Game.sceneInfo.sMapDefine.instanceType && (Game.sceneInfo.sMapDefine.instanceType.value() == EMapInstanceType._EMapInstanceTypeBattlefield || Game.sceneInfo.sMapDefine.instanceType.value() == EMapInstanceType._EMapInstanceTypeGuild)))
                {
                    this.spaEntrance.show();
                }
            }
            
        }

        public function onSpaClose(param1) : void
        {
            this.spaHintButton.hide();
            this.spaInteractPanel.hide();
            
        }

        public function onIteractNumChange(param1:MessageBlock) : void
        {
            this.spaInteractPanel.setNumLabel(param1.messageBase as SSpringInteractCount);
            
        }

        public function onStageResize(event:Event = null) : void
        {
            if (this._spaHintButton)
            {
                this._spaHintButton.x = Global.stage.stageWidth - 395;
                this._spaHintButton.y = 10;
            }
            if (this._spaInteractPanel)
            {
                this._spaInteractPanel.x = Global.stage.stageWidth - 210 - 250;
                this._spaInteractPanel.y = Global.stage.stageHeight - 180;
            }
            
        }

    }
}
